package com.midea.ai.air.ui.home.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.ui.home.data.DeviceManager;

import java.util.List;

/**
 * Created by ouyangdi on 2016/4/9.
 */
public class DeviceManagerAdapter extends ArrayAdapter<DeviceManager>{

    private LayoutInflater mInflater;
    public static final int VIEW_SEPERATOR = 0;
    public static final int VIEW_NORMAL = 1;
    private Context mContext;
    private int mColorOn, mColorOff;

    public DeviceManagerAdapter(Context context, int resource, List<DeviceManager> faultCodes) {
        super(context, resource, faultCodes);
        mInflater = LayoutInflater.from(context);
        mColorOn = context.getResources().getColor(R.color.text_black_standard);
        mColorOff = context.getResources().getColor(R.color.text_gray_assist);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DeviceManager item = getItem(position);
        ViewHolder holder = null;
        int type = getItemViewType(position);

        if(convertView == null) {
            switch (type) {
                case VIEW_SEPERATOR:
                    convertView = mInflater.inflate(R.layout.item_list_divider, parent, false);
                    break;
                case VIEW_NORMAL:
                    convertView = mInflater.inflate(R.layout.item_device_manager, parent, false);
                    holder = new ViewHolder();
                    holder.tvDevice = (TextView) convertView.findViewById(R.id.tv_device);
                    holder.tvDeviceType = (TextView) convertView.findViewById(R.id.tv_device_type);
                    holder.ivDeviceStatus = (ImageView) convertView.findViewById(R.id.iv_device_status);
                    holder.botLine = convertView.findViewById(R.id.bottom_line);
                    convertView.setTag(holder);
                    break;
            }
        }

        else {
            if(type == VIEW_NORMAL) {
                holder = (ViewHolder) convertView.getTag();
            }
        }
        if(type == VIEW_NORMAL && holder != null) {
            holder.tvDevice.setText(item.getStrDevice());
            holder.tvDeviceType.setText(item.getStrDeviceType());

            if(item.isDeviceOn()) {
                holder.ivDeviceStatus.setImageResource(R.drawable.air_edit_room_device_manager_select);
                holder.tvDevice.setTextColor(mColorOn);
            } else {
                holder.ivDeviceStatus.setImageResource(R.drawable.air_edit_room_device_manager);
                holder.tvDevice.setTextColor(mColorOff);
            }

            if (position== getCount()-1) {
                holder.botLine.setVisibility(View.INVISIBLE);
            } else {
                holder.botLine.setVisibility(View.VISIBLE);
            }

            if(position+1 < getCount() ) {
                if (getItem(position+1) == null) {
                    holder.botLine.setVisibility(View.INVISIBLE);
                } else {
                    holder.botLine.setVisibility(View.VISIBLE);
                }
            }
        }

        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        DeviceManager item = getItem(position);
        if(item == null) {
            return VIEW_SEPERATOR;
        }
        return VIEW_NORMAL;
    }

    public class ViewHolder {
        public TextView tvDevice; //家电类型
        public TextView tvDeviceType;  //家电型号
        public ImageView ivDeviceStatus;  //是否开启
        public View botLine; //下划线
    }

    /**
     *设置分隔区不可点击
     */
    @Override
    public boolean isEnabled(int position) {
        int type = getItemViewType(position);
        return (type != VIEW_SEPERATOR);
    }
}
